/**
 * Created by FDG282 on 2017/9/5.
 */
// 地址三级联动
var reg = /^1(0|1|2|3|4|5|6|7|8|9)\d{9}$/;
function address() {
    //_init_area();
}
// 单选框选中状态
function radioChecked() {
    $(".sex_input").children('input[type="radio"]').click(function(){
        $(this).siblings("span").addClass("radio-active").parents(".sex_input").siblings(".sex_input").find('span').removeClass("radio-active");
    });
}
// 表单提交
function formSubimt() {
    $(".user_no_bindwechat .js_phone").val('').removeClass("borshow");
    var formBox = $(".user_phone_sure");
    var $noBindWechat = $(".user_no_bindwechat");

    if($(".user_center_form").find($noBindWechat).length==1){ //微信绑定已经舍弃了
        $(".js_submit").bind("click",function(){
            var phoneVal = formBox.find($(".js_phone")).val();
            var imgCodeVal = formBox.find($(".js_img_code")).val();
            var phoneCodeVal = formBox.find($(".js_phone_code")).val();
            var usernameVal = $(".username_value").val();
            var sexVal = $(".radio-active").siblings('input[type="radio"]').val();
            var EmailVal = $(".email_value").val();
            var bothVal = $('select[name="year"]').val()+"年"+ $('select[name="month"]').val()+"月"+$('select[name="day"]').val()+"日";
            var addressVal = $('select[name="s_province"]').val()+"省"
                + $('select[name="s_city"]').val()+"市"
                +$('select[name="s_county"]').val()+"县/区"
                +$(".address_value").val();
            var province_code = $('select[name="province_id"]').val();
            var province_name = $('select[name="province_id"]').find("option:selected").text();

            var city_code = $('select[name="city_id"]').val();
            var city_name = $('select[name="city_id"]').find("option:selected").text();

            var district_code = $('select[name="area_id"]').val();
            var district_name = $('select[name="area_id"]').find("option:selected").text();

            var adddress = $(".address_value").val();

            if((phoneVal,imgCodeVal,phoneCodeVal,usernameVal,sexVal,EmailVal)!==""){
                $.post($('#validate-form').attr('action'),{
                    mobile:phoneVal,
                    imgCodeVal:imgCodeVal,
                    mobile_code:phoneCodeVal,
                    sex:sexVal,
                    email:EmailVal,
                    birthday:$('select[name="year"]').val()+$('select[name="month"]').val()+$('select[name="day"]').val(),
                    province_code:province_code,
                    province_name:province_name,
                    city_code:city_code,
                    city_name:city_name,
                    district_code:district_code,
                    district_name:district_name,
                    adddress:adddress
                },success,'json');
                return false;
                function success(data){
                    if(data.code == 200){
                        $.layerShow("个人资料保存成功！");
                        window.location.href = data.url;
                    }else{
                        //$('#captchaform-verifycode-image').trigger('click');
                        $(".tipinfo").text(data.val);
                    }
                }
            }
        })
    }else {
        $(".js_submit").bind("click",function(){
            var usernameVal = $(".username_value").val();
            var sexVal = $(".radio-active").siblings('input[type="radio"]').val();
            var EmailVal = $(".email_value").val();
            var bothVal = $('select[name="year"]').val()+"年"+ $('select[name="month"]').val()+"月"+$('select[name="day"]').val()+"日";
            var province_code = $('select[name="province_id"]').val();
            var province_name = $('select[name="province_id"]').find("option:selected").text();

            var city_code = $('select[name="city_id"]').val();
            var city_name = $('select[name="city_id"]').find("option:selected").text();

            var district_code = $('select[name="area_id"]').val();
            var district_name = $('select[name="area_id"]').find("option:selected").text();

            var adddress = $(".address_value").val();

            if((usernameVal,sexVal,EmailVal)!==""){

                $.post($('#validate-form').attr('action'),{
                    username:usernameVal,
                    sex:sexVal,
                    email:EmailVal,
                    birthday:$('select[name="year"]').val()+'-'+$('select[name="month"]').val()+'-'+$('select[name="day"]').val(),
                    province_code:province_code,
                    province_name:province_name,
                    city_code:city_code,
                    city_name:city_name,
                    district_code:district_code,
                    district_name:district_name,
                    adddress:adddress
                },success,'json');
                //return false;
                function success(data){
                    if(data.code == 200){
                        $.layerShow("个人资料保存成功！");
                        window.location.href = data.url;
                    }else{
                        $.each(data.val,function(e,object){
                            $(".tipinfo").text(object[0]);
                        });

                    }
                }
            }
        })
    }

}


$(function () {

    //address();
    radioChecked();
    formSubimt();
});

